﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.BIZ;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using Microsoft.ApplicationBlocks.Data;
namespace AnyOffice.general.system.workflow.flow_type
{
     public partial class type_data : ModulePage
    {

        protected void btnData_Click(object sender, EventArgs e)
        {
            string commandText = "SELECT [RunId] FROM [dbo].[Flow_Run] WHERE [FlowId] = @FlowId AND ((@BeginRunId IS NULL OR [RunId] >= @BeginRunId) AND (@EndRunId IS NULL OR [RunId] <= @EndRunId)) ";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@FlowId", SqlDbType.Int);
            commandParameters[0].Value = Convert.ToInt32(this.ViewState["FlowId"].ToString());
            commandParameters[1] = new SqlParameter("@BeginRunId", SqlDbType.Int);
            if (this.txtBeginRunId.Text != string.Empty)
            {
                commandParameters[1].Value = Convert.ToInt32(this.txtBeginRunId.Text);
            }
            else
            {
                commandParameters[1].Value = DBNull.Value;
            }
            commandParameters[2] = new SqlParameter("@EndRunId", SqlDbType.Int);
            if (this.txtEndRunId.Text != string.Empty)
            {
                commandParameters[2].Value = Convert.ToInt32(this.txtEndRunId.Text);
            }
            else
            {
                commandParameters[2].Value = DBNull.Value;
            }
            string str2 = string.Empty;
            int num = Convert.ToInt32(this.txtItemId.Text);
            int num2 = Convert.ToInt32(this.txtItemNum.Text);
            FlowRunDataDao dao = new FlowRunDataDao();
            DataTable table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText, commandParameters).Tables[0];
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < 1; i++)
                {
                    int num4 = Convert.ToInt32(table.Rows[i]["RunId"].ToString());
                    str2 = str2 + num4.ToString() + ",";
                    DataTable table2 = dao.SelectByRunId(base.Conn, num4);
                    SortedList list = new SortedList();
                    if (this.ddlItemOp.SelectedValue == "add")
                    {
                        bool flag = false;
                        for (int k = 0; k < table2.Rows.Count; k++)
                        {
                            if (num == 0)
                            {
                                if (!flag)
                                {
                                    for (int m = 1; m <= num2; m++)
                                    {
                                        if (!list.Contains(m.ToString()))
                                        {
                                            list.Add(m.ToString(), string.Empty);
                                        }
                                    }
                                }
                                flag = true;
                                if (!list.Contains(Convert.ToString((int) (Convert.ToInt32(table2.Rows[k]["ItemId"].ToString()) + num2))))
                                {
                                    list.Add(Convert.ToString((int) (Convert.ToInt32(table2.Rows[k]["ItemId"].ToString()) + num2)), table2.Rows[k]["ItemData"].ToString());
                                }
                            }
                            else if (num >= Convert.ToInt32(table2.Rows[table2.Rows.Count - 1]["ItemId"].ToString()))
                            {
                                if (!list.Contains(table2.Rows[k]["ItemId"].ToString()))
                                {
                                    list.Add(table2.Rows[k]["ItemId"].ToString(), table2.Rows[k]["ItemData"].ToString());
                                }
                                if (!flag && (k == (table2.Rows.Count - 1)))
                                {
                                    for (int n = 1; n <= num2; n++)
                                    {
                                        if (!list.Contains(Convert.ToString((int) (Convert.ToInt32(table2.Rows[table2.Rows.Count - 1]["ItemId"].ToString()) + n))))
                                        {
                                            list.Add(Convert.ToString((int) (Convert.ToInt32(table2.Rows[table2.Rows.Count - 1]["ItemId"].ToString()) + n)), string.Empty);
                                        }
                                    }
                                    flag = true;
                                }
                            }
                            else if (num > Convert.ToInt32(table2.Rows[k]["ItemId"].ToString()))
                            {
                                if (!list.Contains(table2.Rows[k]["ItemId"].ToString()))
                                {
                                    list.Add(table2.Rows[k]["ItemId"].ToString(), table2.Rows[k]["ItemData"].ToString());
                                }
                            }
                            else if (Convert.ToInt32(table2.Rows[k]["ItemId"].ToString()) == num)
                            {
                                if (!list.Contains(table2.Rows[k]["ItemId"].ToString()))
                                {
                                    list.Add(table2.Rows[k]["ItemId"].ToString(), table2.Rows[k]["ItemData"].ToString());
                                }
                                if (!flag)
                                {
                                    for (int num8 = 1; num8 <= num2; num8++)
                                    {
                                        if (!list.Contains(Convert.ToString((int) (Convert.ToInt32(table2.Rows[k]["ItemId"].ToString()) + num8))))
                                        {
                                            list.Add(Convert.ToString((int) (Convert.ToInt32(table2.Rows[k]["ItemId"].ToString()) + num8)), string.Empty);
                                        }
                                    }
                                    flag = true;
                                }
                            }
                            else if (!list.Contains(Convert.ToString((int) (Convert.ToInt32(table2.Rows[k]["ItemId"].ToString()) + num2))))
                            {
                                list.Add(Convert.ToString((int) (Convert.ToInt32(table2.Rows[k]["ItemId"].ToString()) + num2)), table2.Rows[k]["ItemData"].ToString());
                            }
                        }
                    }
                    if (this.ddlItemOp.SelectedValue == "delete")
                    {
                        for (int num9 = 0; num9 < table2.Rows.Count; num9++)
                        {
                            Response.Write(table2.Rows[num9]["ItemId"].ToString() + ":" + table2.Rows[num9]["ItemData"].ToString() + "<br>");
                        }
                        if (num == 0)
                        {
                            int num10 = 1;
                            for (int num11 = num2; num11 < table2.Rows.Count; num11++)
                            {
                                if (!list.Contains(num10.ToString()))
                                {
                                    list.Add(num10.ToString(), table2.Rows[num11]["ItemData"].ToString());
                                }
                                num10++;
                            }
                        }
                        else if (num < Convert.ToInt32(table2.Rows[table2.Rows.Count - 1]["ItemId"].ToString()))
                        {
                            int num12 = 1;
                            for (int num13 = 0; num13 < table2.Rows.Count; num13++)
                            {
                                if (num > Convert.ToInt32(table2.Rows[num13]["ItemId"].ToString()))
                                {
                                    if (!list.Contains(num12.ToString()))
                                    {
                                        list.Add(num12.ToString(), table2.Rows[num13]["ItemData"].ToString());
                                    }
                                    num12++;
                                }
                                else if (Convert.ToInt32(table2.Rows[num13]["ItemId"].ToString()) == num)
                                {
                                    num13 += num2;
                                }
                                else
                                {
                                    if (!list.Contains(num12.ToString()))
                                    {
                                        list.Add(num12.ToString(), table2.Rows[num13]["ItemData"].ToString());
                                    }
                                    num12++;
                                }
                            }
                        }
                    }
                    Response.Write("<br>");
                    for (int j = 0; j < list.Count; j++)
                    {
                        FlowRunDataData data = new FlowRunDataData();
                        data.RunId = num4;
                        data.ItemId = Convert.ToInt32(list.GetKey(j).ToString());
                        data.ItemData = list[list.GetKey(j).ToString()].ToString();
                        Response.Write(list.GetKey(j).ToString() + ":" + list[list.GetKey(j).ToString()].ToString() + "<br>");
                    }
                }
            }
            this.pnlMain.Visible = false;
            this.litMessage.Text = base.ShowPrompt("共匹配了 " + table.Rows.Count.ToString() + " 项工作的表单数据，流水号为 " + str2, "type_data.aspx?flow_id=" + this.ViewState["FlowId"].ToString()).Replace("180", "300");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            DateTime time = Convert.ToDateTime("1999-1-1");
            DateTime time3 = DateTime.Now.AddDays(1.0);
            if (this.txtBeginDate.Value != string.Empty)
            {
                time = Convert.ToDateTime(this.txtBeginDate.Value);
            }
            if (this.txtEndDate.Value != string.Empty)
            {
                time3 = Convert.ToDateTime(this.txtEndDate.Value);
            }
            int num = new WorkFlowBiz().DeleteForData(base.Conn, Convert.ToInt32(this.ViewState["FlowId"].ToString()), time, time3);
            this.pnlMain.Visible = false;
            this.litMessage.Text = base.ShowPrompt("共删除 " + num.ToString() + " 项工作的数据", "type_data.aspx?flow_id=" + this.ViewState["FlowId"].ToString());
        }
         /*
        private void DeleteFlow(int runId)
        {
            string commandText = "DELETE FROM [dbo].[Flow_Run_Data] WHERE [RunId] = " + runId.ToString();
            SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, commandText);
        }*/

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int num = (Request.QueryString["flow_id"] == null) ? 0 : int.Parse(Request.QueryString["flow_id"].ToString());
                this.ViewState["FlowId"] = num;
                DataTable table = new FlowTypeDao().SelectByFlowId(base.Conn, num);
                if (table.Rows.Count > 0)
                {
                    this.litFlowName.Text = table.Rows[0]["FlowName"].ToString();
                }
                this.btnDelete.Attributes.Add("onclick", "return my_delete()");
                this.btnData.Attributes.Add("onclick", "return my_data()");
            }
        }
       
    }
}

